package cn.swpu.view.action;

import cn.swpu.bose.BoseAction;
import cn.swpu.domin.Department;
import cn.swpu.util.DepartmentUtils;
import com.opensymphony.xwork2.ActionContext;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


import java.util.List;

/**
 * Created by 11616 on 2016/7/18.
 */
@Controller
@Scope("prototype")
public class DepartmentAction extends BoseAction<Department>{

    private Long parentId;

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }


        /**
         * 列表
         * @return
         * @throws Exception
         */
    public  String  list() throws  Exception{
        List<Department> departmentList = null;
        if(parentId == null){
            departmentList = departmentService.findTopAll();
        }else{
            departmentList = departmentService.findChildrenAll(parentId);
            //返回上一级按键所需要
            Department parent = departmentService.findById(parentId);
            ActionContext.getContext().put("parent",parent);
        }
        ActionContext.getContext().put("departmentList",departmentList);
        return  "list";
    }
        /**
         * 添加
         * @return
         * @throws Exception
         */
        public  String  add() throws  Exception{
            //通过parentId寻找到关联的上级
            Department department = departmentService.findById(getParentId());
            if (department != null) {
                model.setParent(department);
            }
            departmentService.save(model);
            return  "tolist";
        }
        /**
         * 添加页面
         * @return
         * @throws Exception
         */
        public  String  addUI() throws  Exception{
            List<Department> topList = departmentService.findTopAll();
            List<Department> departmentList = DepartmentUtils.getAllDeparments(topList);
            ActionContext.getContext().put("departmentList",departmentList);

            return "saveUI";
        }
        /**
         * 修改
         * @return
         * @throws Exception
         */
        public  String  edit() throws  Exception{
            //从数据库中取出原对象
            Department department = departmentService.findById(model.getId());
            //设置修改的属性值
            department.setName(model.getName());
            department.setDescription(model.getDescription());
            //与上级部门进行关联
            department.setParent(departmentService.findById(parentId));
            //更新数据库
            departmentService.update(department);
            return  "tolist";
        }
        /**
         * 修改页面
         * @return
         * @throws Exception
         */
        public  String  editUI() throws  Exception{
            List<Department> departmentList = departmentService.findAll();
            ActionContext.getContext().put("departmentList",departmentList);
            //回显数据
            Department department = departmentService.findById(model.getId());
            ActionContext.getContext().getValueStack().push(department);
            if(department.getParent() != null){
                parentId = department.getParent().getId();
            }
            return  "saveUI";
        }
        /**
         * 删除
         * @return
         * @throws Exception
         */
        public  String  delete() throws  Exception{
            //解决在点击删除按键获取parentId,本来可以在jsp中删除按键那添加（& parentId=%{parentId}），但出错，所以在此处添加parentId值
            Department department = departmentService.findById(model.getId());
            if(department.getParent() != null){
                parentId = department.getParent().getId();
            }
            departmentService.delete(model.getId());
            return "tolist";
        }

}
